<?php

namespace App\Http\Controllers;

use App\Models\TestHousing;
use Illuminate\Http\Request;
use App\Common\Paginate;
use App\Common\Responses;
use App\Common\ValidatorCheck;
use Illuminate\Support\Facades\Validator;

class TestHousingController
{

    /**
     * 获取房源
     * */
    public function getIndex(Request $request)
    {
        try {
            $params['page'] = (int)$request->get('page', 1);
            $params['limit'] = (int)$request->get('limit', 10);
            $params['house_name'] = (string)$request->get('house_name', "");
            $params['price'] = (float)$request->get('price', 0);
            list($limit, $offset) = Paginate::paginate($params['page'], $params['limit']);
            $data = TestHousing::getHouse($params, $limit, $offset);
            $data['page'] = (int)$params['page'];

            return Responses::success($data);
        } catch (\Exception $exception) {
            return Responses::error([], 0, $exception->getMessage());
        }
    }

    /**
     * 添加房源
     * */
    public function createHouse(Request $request)
    {
        try {
            $params = $request->post();
            ValidatorCheck::paramsCheck($params, [
                'uid' => 'required|integer',
                'agent_id' => 'required|integer',
                'location' => 'required',
                'is_agent' => 'required|integer',
                'estate_id' => 'required|integer',
                'region' => 'required',
                'area' => 'required|numeric',
                'price' => 'required|numeric',
                'title' => 'required',
                'content' => 'required',
            ]);

            $data = TestHousing::createHouse($params);
            return Responses::success($data, 20000);
        } catch (\Exception $exception) {
            return Responses::error([], 0, $exception->getMessage());
        }
    }

    /**
     * 获取房源详情
     * */
    public function detailHouse(Request $request)
    {
        try {
            $params['id'] = (int)$request->get('id') ?: null;
            ValidatorCheck::paramsCheck($params, [
                'id' => 'required|integer',
            ]);
            $data = TestHousing::detailHouse([['is_del', '=', 0], ['id', '=', $params['id']]]);
            return Responses::success($data, 20000);
        } catch (\Exception $exception) {
            return Responses::error([], 0, $exception->getMessage());
        }
    }

    /**
     * 修改房源
     * */
    public function updateHouse(Request $request)
    {
        try {
            $params = $request->post();

            ValidatorCheck::paramsCheck($params, [
                'uid' => 'required|integer',
                'agent_id' => 'required|integer',
                'location' => 'required',
                'is_agent' => 'required|integer',
                'estate_id' => 'required|integer',
                'region' => 'required',
                'area' => 'required|numeric',
                'price' => 'required|numeric',
                'title' => 'required',
                'content' => 'required',
            ]);

            $data = TestHousing::updateHouse([['is_del', '=', '0'], ['id', '=', $params['id']]], $params);
            return Responses::success($data, 20000);
        } catch (\Exception $exception) {
            return Responses::error([], 0, $exception->getMessage());
        }
    }

    /**
     * 删除房源
     * */
    public function delHouse(Request $request)
    {
        try {
            $params['id'] = (int)$request->get('id') ?: null;
            ValidatorCheck::paramsCheck($params, [
                'id' => 'required|integer',
            ]);
            $data = TestHousing::updateHouse([['is_del', '=', '0'], ['id', '=', $params['id']]], ['is_del' => 1]);
            return Responses::success($data, 20000);
        } catch (\Exception $exception) {
            return Responses::error([], 0, $exception->getMessage());
        }
    }


}
